<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <link rel="stylesheet" media="screen" type="text/css" href="./style.css" />
  <link rel="stylesheet" media="screen" type="text/css" href="./design.css" />
  <link rel="stylesheet" media="print" type="text/css" href="./print.css" />

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="dokuwiki export">

<p>
<em>Translations of this page are also available in the following languages:</em> <a href="geda-guile_scripting.ru.html" class="wikilink1" title="geda-guile_scripting.ru.html">Русский</a>.
</p>

<h2 class="sectionedit1"><a name="guile_scripting" id="guile_scripting">Guile scripting</a></h2>
<div class="level2">

<p>
gEDA/gaf uses <a href="http://www.gnu.org/s/guile/" class="urlextern" title="http://www.gnu.org/s/guile/"  rel="nofollow">Guile Scheme</a> to provide Scheme scripting capabilities, and all of the features of Guile are available to use.  The <em>Guile Reference Manual</em> is available as an Info manual (<code>info guile</code> on most systems), or <a href="http://www.gnu.org/software/guile/docs/docs.html" class="urlextern" title="http://www.gnu.org/software/guile/docs/docs.html"  rel="nofollow">on the Guile website</a>.
</p>

<p>
A collection of modules is provided for accessing and modifying gEDA objects and pages, called the gEDA Scheme <acronym title="Application Programming Interface">API</acronym>.  The <em>gEDA Scheme Reference Manual</em> is also available as an Info manual (<code>info geda-scheme</code>).
</p>

</div>
<!-- EDIT1 SECTION "Guile scripting" [118-733] -->
<h3 class="sectionedit2"><a name="tutorials" id="tutorials">Tutorials</a></h3>
<div class="level3">
<ul>
<li class="level1"><div class="li"> <a href="geda-gnetlist_scheme_tutorial.html" class="wikilink1" title="geda-gnetlist_scheme_tutorial.html">Scripting a gnetlist backend in scheme</a> (John Doty)</div>
</li>
</ul>

<p>
See also the <a href="geda-gschem_ug-extensions.html" class="wikilink1" title="geda-gschem_ug-extensions.html">Extending gschem</a> section of the <em><a href="geda-gschem_ug.html" class="wikilink1" title="geda-gschem_ug.html">gEDA gschem User Guide</a></em>, and <a href="geda-gnetlist_ug.html#scheme_backend_api" class="wikilink1" title="geda-gnetlist_ug.html">Scheme Backend API</a> section of the <em><a href="geda-gnetlist_ug.html" class="wikilink1" title="geda-gnetlist_ug.html">gEDA gnetlist User Guide</a></em>.
</p>

</div>
<!-- EDIT2 SECTION "Tutorials" [734-1091] -->
<h3 class="sectionedit3"><a name="reference_documents" id="reference_documents">Reference documents</a></h3>
<div class="level3">
<ul>
<li class="level1"><div class="li"> <a href="geda-gnetlist_scheme_primitives.html" class="wikilink1" title="geda-gnetlist_scheme_primitives.html">gnetlist Scheme primitives</a></div>
</li>
</ul>

</div>
<!-- EDIT3 SECTION "Reference documents" [1092-1157] -->
<h3 class="sectionedit4"><a name="scripting_examples" id="scripting_examples">Scripting examples</a></h3>
<div class="level3">

<p>
You can download each script example and load it in <strong>gschem</strong>:
</p>
<ul>
<li class="level1"><div class="li"> just hit <kbd>:</kbd> and enter <pre class="code lisp"><span class="br0">&#40;</span>load <span class="st0">&quot;filename.scm&quot;</span><span class="br0">&#41;</span></pre>
</div>
</li>
<li class="level1"><div class="li"> then hit <kbd>Enter</kbd></div>
</li>
</ul>

</div>

<h4><a name="removing_objects_with_specific_properties" id="removing_objects_with_specific_properties">Removing objects with specific properties</a></h4>
<div class="level4">

<p>
For instance, let&#039;s remove all objects which are circles or arcs
with zero radius:
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=1" title="Download Snippet" class="mediafile mf_scm">remove-objects.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda page<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Checks if the OBJECT is a circle or an arc with zero radius</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>zero-radius-object? object<span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="kw1">or</span>
    <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>circle? object<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">=</span> <span class="br0">&#40;</span>circle-radius object<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>arc?    object<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="sy0">=</span> <span class="br0">&#40;</span>arc-radius    object<span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="br0">&#40;</span><span class="kw1">apply</span> page-<span class="kw1">remove</span><span class="sy0">!</span> <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span>
       <span class="br0">&#40;</span>filter
         zero-radius-object?
         <span class="br0">&#40;</span>page-contents <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
Let&#039;s suppose we have a component with a known attribute to
remove, then we have to detach and remove all its attributes, too.
The function below does exactly this.
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=2" title="Download Snippet" class="mediafile mf_scm">remove-components-with-attribs.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda page<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda object<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda attrib<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Removes all components having the attrib NAME=VALUE from PAGE</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>delete-components-by-attrib<span class="sy0">!</span> page <span class="kw1">name</span> <span class="kw1">value</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>for-each
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>obj<span class="br0">&#41;</span>
      <span class="br0">&#40;</span><span class="kw1">if</span> <span class="br0">&#40;</span>component? obj<span class="br0">&#41;</span>
        <span class="br0">&#40;</span>for-each
          <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>attr<span class="br0">&#41;</span>
            <span class="br0">&#40;</span><span class="kw1">and</span>
              <span class="br0">&#40;</span>string<span class="sy0">=</span>? <span class="br0">&#40;</span>attrib-<span class="kw1">name</span> attr<span class="br0">&#41;</span> <span class="kw1">name</span><span class="br0">&#41;</span>
              <span class="br0">&#40;</span>string<span class="sy0">=</span>? <span class="br0">&#40;</span>attrib-<span class="kw1">value</span> attr<span class="br0">&#41;</span> <span class="kw1">value</span><span class="br0">&#41;</span>
              <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>attached-attribs <span class="br0">&#40;</span>object-attribs obj<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
                <span class="br0">&#40;</span><span class="kw1">apply</span> detach-attribs<span class="sy0">!</span> obj attached-attribs<span class="br0">&#41;</span>
                <span class="br0">&#40;</span><span class="kw1">apply</span> page-<span class="kw1">remove</span><span class="sy0">!</span> page obj attached-attribs<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
          <span class="br0">&#40;</span>object-attribs obj<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>page-contents page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
After loading the file, hit <kbd>:</kbd> and enter, for example,
</p>
<pre class="code lisp"><span class="br0">&#40;</span>delete-components-by-attrib<span class="sy0">!</span> <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span> <span class="st0">&quot;refdes&quot;</span> <span class="st0">&quot;R1&quot;</span><span class="br0">&#41;</span></pre>

</div>

<h4><a name="procedures_for_input-output" id="procedures_for_input-output">Procedures for input-output</a></h4>
<div class="level4">

<p>
The following script defines two procedures that can be used in
<strong>gaf shell</strong> batch scripts:
</p>
<ul>
<li class="level1"><div class="li"> <code>schematic-file→page</code></div>
</li>
<li class="level1"><div class="li"> <code>page→schematic-file</code></div>
</li>
</ul>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=4" title="Download Snippet" class="mediafile mf_scm">geda-io.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>ice-<span class="nu0">9</span> lineio<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>geda page<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Input/output procedures</span>
<span class="co1">; reads FILE and outputs string</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>file-<span class="sy0">&gt;</span>string file<span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="kw1">let</span>* <span class="br0">&#40;</span><span class="br0">&#40;</span>port <span class="br0">&#40;</span>make-line-buffering-input-port <span class="br0">&#40;</span>open-file file <span class="st0">&quot;r&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span><span class="kw1">do</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>line <span class="st0">&quot;&quot;</span> <span class="br0">&#40;</span>read-string port<span class="br0">&#41;</span><span class="br0">&#41;</span>
         <span class="br0">&#40;</span>s <span class="st0">&quot;&quot;</span> <span class="br0">&#40;</span>string-<span class="kw1">append</span> s line<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
      <span class="br0">&#40;</span><span class="br0">&#40;</span>eof-object? line<span class="br0">&#41;</span> <span class="co1">; test</span>
       <span class="br0">&#40;</span>close-port port<span class="br0">&#41;</span>  <span class="co1">; expression(s) to evaluate in the end</span>
       s<span class="br0">&#41;</span>                 <span class="co1">; return value</span>
      <span class="co1">; empty body</span>
      <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; reads schematic FILE and outputs PAGE object</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>schematic-file-<span class="sy0">&gt;</span>page file<span class="br0">&#41;</span>
    <span class="br0">&#40;</span>string-<span class="sy0">&gt;</span>page file <span class="br0">&#40;</span>file-<span class="sy0">&gt;</span>string file<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; saves schematic PAGE to FILE</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>page-<span class="sy0">&gt;</span>schematic-file page file<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>with-output-to-file file
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#40;</span>display <span class="br0">&#40;</span>page-<span class="sy0">&gt;</span>string page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

</div>

<h4><a name="copy_move_and_rotate_objects" id="copy_move_and_rotate_objects">Copy, move, and rotate objects</a></h4>
<div class="level4">
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=5" title="Download Snippet" class="mediafile mf_scm">move-and-rotate.scm</a></dt>
<dd><pre class="code file lisp"><span class="co1">; Scripting example by vzh per request of Kai-Martin Knaak :-)</span>
<span class="co1">; Use at your own risk.</span>
&nbsp;
<span class="co1">; The main procedure here is</span>
<span class="co1">; multiple-copy-move-and-rotate-selection which can be abbreviated</span>
<span class="co1">; as mcmars.</span>
<span class="co1">; Usage:</span>
<span class="co1">;   launch gschem so it can use this script, e.g.</span>
<span class="co1">;     gschem -s move-and-rotate.scm</span>
<span class="co1">;   select objects in gschem, then hit ':' (semicolon) and type</span>
<span class="co1">;     (mcmars '(1000 . 500) 90 10)</span>
<span class="co1">;   hit &lt;Enter&gt;</span>
<span class="co1">; Enjoy!</span>
&nbsp;
&nbsp;
<span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; align coords by ALIGN</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>ceiling-coords vector align<span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="kw1">cons</span>
    <span class="br0">&#40;</span>* <span class="br0">&#40;</span>ceiling-quotient <span class="br0">&#40;</span><span class="kw1">car</span> vector<span class="br0">&#41;</span> align<span class="br0">&#41;</span> align<span class="br0">&#41;</span>
    <span class="br0">&#40;</span>* <span class="br0">&#40;</span>ceiling-quotient <span class="br0">&#40;</span><span class="kw1">cdr</span> vector<span class="br0">&#41;</span> align<span class="br0">&#41;</span> align<span class="br0">&#41;</span>
    <span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Get minimum X and minimum Y of two pairs of coords</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>min-coords coord1 coord2<span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>x <span class="br0">&#40;</span><span class="kw1">min</span> <span class="br0">&#40;</span><span class="kw1">car</span> coord1<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">car</span> coord2<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
        <span class="br0">&#40;</span>y <span class="br0">&#40;</span><span class="kw1">min</span> <span class="br0">&#40;</span><span class="kw1">cdr</span> coord1<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cdr</span> coord2<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="co1">; return value</span>
    <span class="br0">&#40;</span><span class="kw1">cons</span> x y<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Copy, move and rotate current selection. The selected objects</span>
<span class="co1">; are first copied, then translated by VECTOR and finally rotated</span>
<span class="co1">; by ANGLE about center which is calculated as rounded by 100</span>
<span class="co1">; lower left coordinate of all objects in selection.</span>
<span class="co1">; If no objects are selected, opens gschem message dialog with</span>
<span class="co1">; warning.</span>
<span class="co1">; Returns the copied objects.</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>copy-move-and-rotate-selection vector angle<span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>objects <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span><span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">null</span>? objects<span class="br0">&#41;</span>
      <span class="br0">&#40;</span>gschem-msg <span class="st0">&quot;Select something first!&quot;</span><span class="br0">&#41;</span>
      <span class="co1">; else</span>
      <span class="br0">&#40;</span><span class="kw1">let</span>* <span class="br0">&#40;</span><span class="br0">&#40;</span>copied-objects <span class="br0">&#40;</span>map copy-object objects<span class="br0">&#41;</span><span class="br0">&#41;</span>
             <span class="br0">&#40;</span>translated-objects <span class="br0">&#40;</span><span class="kw1">apply</span> translate-objects<span class="sy0">!</span> vector copied-objects<span class="br0">&#41;</span><span class="br0">&#41;</span>
             <span class="br0">&#40;</span>bounds <span class="br0">&#40;</span><span class="kw1">apply</span> object-bounds translated-objects<span class="br0">&#41;</span><span class="br0">&#41;</span>
             <span class="br0">&#40;</span>rotation-center <span class="br0">&#40;</span>ceiling-coords <span class="br0">&#40;</span>min-coords <span class="br0">&#40;</span><span class="kw1">car</span> bounds<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">cdr</span> bounds<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="nu0">100</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
             <span class="br0">&#40;</span>rotated-objects <span class="br0">&#40;</span><span class="kw1">apply</span> rotate-objects<span class="sy0">!</span> rotation-center angle translated-objects<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
        <span class="br0">&#40;</span><span class="kw1">apply</span> page-<span class="kw1">append</span><span class="sy0">!</span> <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span> rotated-objects<span class="br0">&#41;</span>
        rotated-objects<span class="br0">&#41;</span>
      <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Multiply VECTOR which must be a pair by NUMBER</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>multiply-vector-by vector number<span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="kw1">cons</span> <span class="br0">&#40;</span>* number <span class="br0">&#40;</span><span class="kw1">car</span> vector<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#40;</span>* number <span class="br0">&#40;</span><span class="kw1">cdr</span> vector<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Copy, move and rotate current selection NUMBER times. Applies</span>
<span class="co1">; the copy-move-and-rotate-selection procedure multiple times</span>
<span class="co1">; increasing every time vector and angle by given values of VECTOR</span>
<span class="co1">; and ANGLE.</span>
<span class="co1">; If no objects are selected, opens gschem message dialog with</span>
<span class="co1">; warning.</span>
<span class="co1">; Return value is unspecified.</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>multiple-copy-move-and-rotate-selection vector angle num<span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">null</span>? <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>gschem-msg <span class="st0">&quot;Select something first!&quot;</span><span class="br0">&#41;</span>
    <span class="co1">; else</span>
    <span class="br0">&#40;</span><span class="kw1">do</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>i num <span class="br0">&#40;</span><span class="nu0">1</span>- i<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
      <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="sy0">=</span> i <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
      <span class="br0">&#40;</span>copy-move-and-rotate-selection
        <span class="br0">&#40;</span>multiply-vector-by vector i<span class="br0">&#41;</span> <span class="br0">&#40;</span>* angle i<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">; Abbreviated name for the multiple-copy-move-and-rotate-selection</span>
<span class="co1">; procedure</span>
<span class="br0">&#40;</span>define mcmars multiple-copy-move-and-rotate-selection<span class="br0">&#41;</span></pre>
</dd></dl>

</div>

<h4><a name="group_attribute_editing" id="group_attribute_editing">Group attribute editing</a></h4>
<div class="level4">

<p>
Let&#039;s suppose you have selected several resistors&#039; refdeses and
want to rename them at once, e.g., if they were copy from another
place.
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=6" title="Download Snippet" class="mediafile mf_">set-selected-attribs-value</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>set-selected-attribs-<span class="kw1">value</span><span class="sy0">!</span> <span class="kw1">value</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span>for-each
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>attrib<span class="br0">&#41;</span>
      <span class="br0">&#40;</span>set-attrib-<span class="kw1">value</span><span class="sy0">!</span> attrib <span class="kw1">value</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
Usage of the procedure in <strong>gschem</strong>:
</p>
<pre class="code lisp"><span class="br0">&#40;</span>set-selected-attribs-<span class="kw1">value</span><span class="sy0">!</span> <span class="st0">&quot;R100.?&quot;</span><span class="br0">&#41;</span></pre>

<p>
Now, after renumbering them using <kbd>T</kbd> <kbd>U</kbd>, you
copy them all and want to rename those copied resistors appending a suffix:
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=8" title="Download Snippet" class="mediafile mf_scm">append-selected-attribs-suffix.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>append-selected-attribs-suffix<span class="sy0">!</span> suffix<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>for-each
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>attrib<span class="br0">&#41;</span>
      <span class="br0">&#40;</span>set-attrib-<span class="kw1">value</span><span class="sy0">!</span>
        attrib
        <span class="br0">&#40;</span>string-<span class="kw1">append</span> <span class="br0">&#40;</span>attrib-<span class="kw1">value</span> attrib<span class="br0">&#41;</span> suffix<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
Usage of the procedure in <strong>gschem</strong>:
</p>
<pre class="code lisp"><span class="br0">&#40;</span>append-selected-attribs-suffix<span class="sy0">!</span> <span class="st0">&quot;-top&quot;</span><span class="br0">&#41;</span></pre>

<p>
Now, let&#039;s rename some other attributes by adding a prefix:
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=10" title="Download Snippet" class="mediafile mf_scm">append-selected-attribs-prefix.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>append-selected-attribs-prefix<span class="sy0">!</span> prefix<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>for-each
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
      <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
           <span class="br0">&#40;</span>set-attrib-<span class="kw1">value</span><span class="sy0">!</span>
             object
             <span class="br0">&#40;</span>string-<span class="kw1">append</span> prefix <span class="br0">&#40;</span>attrib-<span class="kw1">value</span> object<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
Usage of the procedure in <strong>gschem</strong>:
</p>
<pre class="code lisp"><span class="br0">&#40;</span>append-selected-attribs-prefix<span class="sy0">!</span> <span class="st0">&quot;A1.&quot;</span><span class="br0">&#41;</span></pre>

<p>
Let&#039;s replace first letters of selected attribs with prefix:
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=12" title="Download Snippet" class="mediafile mf_scm">append-selected-attribs-prefix.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>replace-selected-attribs-prefix<span class="sy0">!</span> prefix<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>for-each
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
      <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
           <span class="br0">&#40;</span>set-attrib-<span class="kw1">value</span><span class="sy0">!</span>
             object
             <span class="br0">&#40;</span>string-<span class="kw1">append</span>
               prefix
               <span class="br0">&#40;</span>string-copy <span class="br0">&#40;</span>attrib-<span class="kw1">value</span> object<span class="br0">&#41;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
Usage of the procedure in <strong>gschem</strong>:
</p>
<pre class="code lisp"><span class="br0">&#40;</span>replace-selected-attribs-prefix<span class="sy0">!</span> <span class="st0">&quot;C&quot;</span><span class="br0">&#41;</span></pre>

<p>
Let&#039;s rename selected <code>netname=</code> attributes increasing them by a
fixed number:
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=14" title="Download Snippet" class="mediafile mf_scm">add-selected-attribs-number.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>add-selected-attribs-number<span class="sy0">!</span> number<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>for-each
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
      <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
           <span class="br0">&#40;</span>set-attrib-<span class="kw1">value</span><span class="sy0">!</span>
             object
             <span class="br0">&#40;</span>number-<span class="sy0">&gt;</span>string
               <span class="br0">&#40;</span>+ <span class="br0">&#40;</span>string-<span class="sy0">&gt;</span>number <span class="br0">&#40;</span>attrib-<span class="kw1">value</span> object<span class="br0">&#41;</span><span class="br0">&#41;</span> number<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
Usage of the procedure in <strong>gschem</strong>:
</p>
<pre class="code lisp"><span class="br0">&#40;</span>add-selected-attribs-number<span class="sy0">!</span> <span class="nu0">100</span><span class="br0">&#41;</span></pre>

<p>
We could set any function instead of ”+” on the net number in this procedure.
For instance:
</p>
<dl class="file">
<dt><a href="_export/code/geda:guile_scripting?codeblock=16" title="Download Snippet" class="mediafile mf_scm">use-another-func.scm</a></dt>
<dd><pre class="code file lisp"><span class="br0">&#40;</span>use-modules <span class="br0">&#40;</span>gschem selection<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>use-another-func<span class="sy0">!</span> func<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>for-each
    <span class="br0">&#40;</span><span class="kw1">lambda</span> <span class="br0">&#40;</span>object<span class="br0">&#41;</span>
      <span class="br0">&#40;</span><span class="kw1">and</span> <span class="br0">&#40;</span>attribute? object<span class="br0">&#41;</span>
           <span class="br0">&#40;</span>set-attrib-<span class="kw1">value</span><span class="sy0">!</span>
             object
             <span class="br0">&#40;</span>number-<span class="sy0">&gt;</span>string
               <span class="br0">&#40;</span>func <span class="br0">&#40;</span>string-<span class="sy0">&gt;</span>number <span class="br0">&#40;</span>attrib-<span class="kw1">value</span> object<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span>page-selection <span class="br0">&#40;</span>active-page<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre>
</dd></dl>

<p>
Usage of the procedure in <strong>gschem</strong>:
</p>
<pre class="code lisp"><span class="br0">&#40;</span>use-another-func<span class="sy0">!</span> -<span class="br0">&#41;</span>
<span class="br0">&#40;</span>define <span class="br0">&#40;</span>multiply-by-<span class="nu0">2</span> x<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>* <span class="nu0">2</span> x<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span>use-another-func<span class="sy0">!</span> multiply-by-<span class="nu0">2</span><span class="br0">&#41;</span></pre>

</div>
<!-- EDIT4 SECTION "Scripting examples" [1158-] --></div>
</body>
</html>
